﻿namespace WMS6.Web.TransportManagement
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.TransportMgtServiceRef;

    public class Tracking : BasePage
    {
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected DropDownList ddlAccpectMode;
        protected HtmlForm F8041;
        protected GridView gvTrackingDetail;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFrom;
        protected Label lblAccpectMode;
        protected Label lblBolID;
        protected Label lblCarrierID;
        protected Label lblDeliveryOrderID;
        protected Label lblDriver;
        protected Label lblDriverPhone;
        protected Label lblEquipmentLicence;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblRemark;
        protected Label lblRoadInfo;
        protected Label lblTrackDate;
        protected Label lblTrackLoc;
        protected Label lblUdf1;
        protected Label lblUdf10;
        protected Label lblUdf2;
        protected Label lblUdf3;
        protected Label lblUdf4;
        protected Label lblUdf5;
        protected Label lblUdf6;
        protected Label lblUdf7;
        protected Label lblUdf8;
        protected Label lblUdf9;
        protected HtmlLink mycss;
        private int num = 0;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtBolID;
        protected TextBox txtCarrierID;
        protected TextBox txtDeliveryOrderID;
        protected TextBox txtDriver;
        protected TextBox txtDriverPhone;
        protected TextBox txtEquipmentLicence;
        protected TextBox txtRemark;
        protected TextBox txtRoadInfo;
        protected TextBox txtTrackDate;
        protected TextBox txtTrackLoc;
        protected TextBox txtUdf1;
        protected TextBox txtUdf10;
        protected TextBox txtUdf2;
        protected TextBox txtUdf3;
        protected TextBox txtUdf4;
        protected TextBox txtUdf5;
        protected TextBox txtUdf6;
        protected TextBox txtUdf7;
        protected TextBox txtUdf8;
        protected TextBox txtUdf9;
        protected UpdatePanel up1;

        private void BindData2TrackingDetailList()
        {
            DataSet ds = new DataSet();
            TrackingDetailQueryEntity trackingDetailQuery = new TrackingDetailQueryEntity {
                IsGetAll = true,
                CurrentPage = "0",
                PageSize = this.gvTrackingDetail.PageSize.ToString(),
                SortField = "ROWID",
                SortDirection = "ASC",
                BolID = string.Format(" = '{0}' ", this.txtBolID.Text.Trim())
            };
            ds = TransportMgtServiceClientProxy.GetInstance().GetTrackingDetailInfoByQueryEntity(trackingDetailQuery);
            base.BindData2GridView(this.gvTrackingDetail, ds);
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("TrackingQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("TrackingList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.InsertTrackingDetail();
        }

        private TrackingDetailInfo GetTrackingDetail(TrackingDetailInfo trackingDetailInfo)
        {
            DateTime tempDateTime = DateTime.Now;
            trackingDetailInfo.BolID = this.txtBolID.Text.Trim();
            trackingDetailInfo.DeliveryOrderID = this.txtDeliveryOrderID.Text.Trim();
            trackingDetailInfo.CarrierID = this.txtCarrierID.Text.Trim();
            trackingDetailInfo.DriverID = this.txtDriver.Text.Trim();
            trackingDetailInfo.DriverPhone = this.txtDriverPhone.Text.Trim();
            if (DateTime.TryParse(this.txtTrackDate.Text, out tempDateTime))
            {
                trackingDetailInfo.TrackingDate = DateTime.Parse(this.txtTrackDate.Text.Trim());
            }
            else
            {
                trackingDetailInfo.TrackingDate = DateTime.Now;
            }
            trackingDetailInfo.AcceptMode = this.ddlAccpectMode.SelectedValue;
            trackingDetailInfo.EquipmentLicence = this.txtEquipmentLicence.Text.Trim();
            trackingDetailInfo.TrackingLoc = this.txtTrackLoc.Text.Trim();
            trackingDetailInfo.RoadInfo = this.txtRoadInfo.Text.Trim();
            trackingDetailInfo.Remark = this.txtRemark.Text.Trim();
            trackingDetailInfo.Udf1 = this.txtUdf1.Text.Trim();
            trackingDetailInfo.Udf2 = this.txtUdf2.Text.Trim();
            trackingDetailInfo.Udf3 = this.txtUdf3.Text.Trim();
            trackingDetailInfo.Udf4 = this.txtUdf4.Text.Trim();
            trackingDetailInfo.Udf5 = this.txtUdf5.Text.Trim();
            trackingDetailInfo.Udf6 = this.txtUdf6.Text.Trim();
            trackingDetailInfo.Udf7 = this.txtUdf7.Text.Trim();
            trackingDetailInfo.Udf8 = this.txtUdf8.Text.Trim();
            trackingDetailInfo.Udf9 = this.txtUdf9.Text.Trim();
            trackingDetailInfo.Udf10 = this.txtUdf10.Text.Trim();
            return trackingDetailInfo;
        }

        private TrackingDetailQueryEntity GetTrackingDetailQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as TrackingDetailQueryEntity);
            }
            return (base.PageQueryEntitySession as TrackingDetailQueryEntity);
        }

        protected void gvTrackingDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F8042", this.gvTrackingDetail);
        }

        protected void gvTrackingDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                this.num++;
                e.Row.Cells[0].Text = this.num.ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvTrackingDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                TrackingDetailQueryEntity trackingDetailQuery = this.GetTrackingDetailQueryEntity();
                trackingDetailQuery.SortField = e.SortExpression;
                if ((trackingDetailQuery.SortDirection == "ASC") || (trackingDetailQuery.SortDirection == ""))
                {
                    trackingDetailQuery.SortDirection = "DESC";
                }
                else if (trackingDetailQuery.SortDirection == "DESC")
                {
                    trackingDetailQuery.SortDirection = "ASC";
                }
                this.BindData2TrackingDetailList();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void InsertTrackingDetail()
        {
            try
            {
                TrackingDetailInfo trackingDetailInfo = new TrackingDetailInfo();
                trackingDetailInfo = this.GetTrackingDetail(trackingDetailInfo);
                trackingDetailInfo.CreatedBy = base.UserInfo.UserID;
                string trackingID = TransportMgtServiceClientProxy.GetInstance().InsertTrackingDetail(trackingDetailInfo);
                base.DataEntityViewState = trackingDetailInfo;
                this.BindData2TrackingDetailList();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlAccpectMode, "AcceptMode");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetTrackingDetail2Form(base.Request.QueryString["ID"]);
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetTrackingDetail2Form(string bolID)
        {
            try
            {
                this.txtBolID.Text = bolID;
                this.txtBolID_TextChanged(null, null);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void txtBolID_TextChanged(object sender, EventArgs e)
        {
            BolDetailQueryEntity bolDetailQuery = new BolDetailQueryEntity {
                BolID = "='" + this.txtBolID.Text.Trim() + "'",
                IsGetAll = false,
                CurrentPage = "0",
                PageSize = "1",
                SortField = "ROWID",
                SortDirection = "ASC"
            };
            DataSet ds = TransportMgtServiceClientProxy.GetInstance().GetBolDetailByQueryEntity(bolDetailQuery);
            if (((ds != null) && (ds.Tables.Count > 0)) && (ds.Tables[2].Rows.Count > 0))
            {
                this.txtDeliveryOrderID.Text = "";
                this.txtDeliveryOrderID.Text = ds.Tables[2].Rows[0]["DELIVERY_ID"].ToString();
                this.txtCarrierID.Text = ds.Tables[2].Rows[0]["CARRIER"].ToString();
                this.txtDriver.Text = ds.Tables[2].Rows[0]["DRIVER"].ToString();
                if (!string.IsNullOrEmpty(this.txtDriver.Text.Trim()))
                {
                    DriverInfo driverInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(this.txtDriver.Text.Trim(), "DRIVER") as DriverInfo;
                    this.txtDriverPhone.Text = driverInfo.Telephone;
                }
                this.txtEquipmentLicence.Text = ds.Tables[2].Rows[0]["ACTUAL_EQUIPMENT"].ToString();
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',1,2);</script>", false);
            }
            else
            {
                this.lblMsg.Text = "装车单不存在!";
                this.txtDeliveryOrderID.Text = "";
                this.txtCarrierID.Text = "";
                this.txtDriver.Text = "";
                this.txtEquipmentLicence.Text = "";
            }
            this.txtBolID.Focus();
            this.BindData2TrackingDetailList();
        }

        private void UpdateTrackingDetail()
        {
            try
            {
                TrackingDetailInfo trackingDetailInfo = base.DataEntityViewState as TrackingDetailInfo;
                trackingDetailInfo = this.GetTrackingDetail(trackingDetailInfo);
                trackingDetailInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = TransportMgtServiceClientProxy.GetInstance().UpdateTrackingDetail(trackingDetailInfo) + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

